﻿//Copyright 2010 http://tv4home.codeplex.com
//This file is part of TV4Home.Client.TVViewer.
//TV4Home.Client.TVViewer is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
//TV4Home.Client.TVViewer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//You should have received a copy of the GNU Lesser General Public License along with TV4Home.Client.TVViewer. If not, see http://www.gnu.org/licenses/.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TV4Home.Server.TVEInteractionLibrary.Interfaces;
using System.ServiceModel;

namespace TV4Home.Client.Desktop.TVViewer.Controls
{
    /// <summary>
    /// Interaktionslogik für TvGuide.xaml
    /// </summary>
    /// 
    public class exChannels
    {
        public WebChannelBasic Channel { get; set; }
        public List<WebProgramBasic> Programs { get; set; }

        public exChannels()
        { }

        public exChannels(WebChannelBasic ch, List<WebProgramBasic> prog)
        {
            this.Channel = ch;
            this.Programs = prog;
        }
    }
    public partial class TvGuide : Page
    {
        #region Attribute

        ITVEInteraction tvWebClient = null;
        List<WebChannelGroup> _channelGroups;
        List<WebSchedule> _schedules = null;
        List<WebChannelBasic> _channels = null;
        string UserNameStream1;
        string UserNameStream2;

        #endregion
        public TvGuide()
        {
            InitializeComponent();
            FillGrid();
        }

        public void FillGrid()
        {
            if (tvWebClient == null)
            {
                ConnectToService();
            }
            if (tvWebClient != null)
            {
                _channels = tvWebClient.GetChannelsBasic(1);

                foreach (var channel in _channels)
                {
                    exChannels exChannel = new exChannels(channel, tvWebClient.GetProgramsBasicForChannel(channel.IdChannel, DateTime.Now, DateTime.Now.AddHours(10)));

                    TVGuide.Items.Add(exChannel);






                }
            }

        }

        public void ConnectToService()
        {
            //Connects to the tv4home webservice by netpipe
            try
            {
                if (true)
                {
                    try
                    {
                        tvWebClient = ChannelFactory<ITVEInteraction>.CreateChannel(new BasicHttpBinding() { MaxReceivedMessageSize = 10000000 }, new EndpointAddress(String.Format(Properties.Settings.Default.IntranetUrl, "localhost")));
                    }
                    catch (CommunicationException ex)
                    {
                        tvWebClient = ChannelFactory<ITVEInteraction>.CreateChannel(new BasicHttpBinding() { MaxReceivedMessageSize = 10000000 }, new EndpointAddress(String.Format(Properties.Settings.Default.IntranetUrl, Properties.Settings.Default.ServerName)));

                    }
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                //if connection is not possible ask whether it retry or not
                string sMessageBoxText = "Verbindung konnte nicht hergestellt werden. Erneut versuchen?";
                string sCaption = "TVViewer";

                MessageBoxButton btnMessageBox = MessageBoxButton.YesNo;
                MessageBoxImage icnMessageBox = MessageBoxImage.Warning;

                MessageBoxResult rsltMessageBox = MessageBox.Show(sMessageBoxText, sCaption, btnMessageBox, icnMessageBox);


                // Process message box results

                switch (rsltMessageBox)
                {

                    case MessageBoxResult.Yes:
                        ConnectToService();

                        break;
                    case MessageBoxResult.No:

                        break;
                }
            }

        }
    }
}
